<?php

namespace app\common\model;

use think\Model;

class Article extends Model {
    protected $pk     = 'art_id';
    protected $table  = 'blog_article';
    protected $auto   = ['admin_id'];
    protected $insert = ['art_indate', 'art_intime'];
    protected $update = ['art_update', 'art_uptime'];

    protected function setAdminIdAttr($value) {
        return session('admin.admin_id');
    }

    protected function setArtIndateAttr($value) {
        return date('Y-m-d');
    }

    protected function setArtIntimeAttr($value) {
        return time();
    }

    protected function setArtUpdateAttr($value) {
        return date('Y-m-d');
    }

    protected function setArtUptimeAttr($value) {
        return time();
    }

    public function store($data) {
        if (!isset($data['tag'])) {
            return ['valid' => 0, 'msg' => '请选择标签!'];
        }
        $art_tag_model = new ArtTag();
        $ret = $this->validate(true)->allowField(true)->save($data);
        if ($ret) {
            $db = [];
            foreach ($data['tag'] as $tag_id) {
                $db[] = [
                    'art_id' => $this->art_id,
                    'tag_id' => $tag_id
                ];
            }
            $art_tag_model->saveAll($db);
            return ['valid' => 1, 'msg' => '操作成功!'];
        } else {
            return ['valid' => 0, 'msg' => $this->getError()];
        }
    }

    // 获取文章数据列表
    public function getAll() {




    }
    
}
